<template>
  <div>
    <h1>用户登录</h1>
    <el-row :gutter="20">
      <el-col :span="5">
        <div class="grid-content ep-bg-purple"></div>
      </el-col>
      <el-col :span="10">
        <el-form :model="form" label-width="120px">
          <el-form-item label="用户名">
            <el-input v-model="user.username"/>
          </el-form-item>
          <el-form-item label="密码">
            <el-input type="password" v-model="user.password"/>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="login">登录</el-button>
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :span="5">
        <div class="grid-content ep-bg-purple"></div>
      </el-col>
    </el-row>
  </div>

</template>

<script>
/* eslint-disable */
// 单独引入ElMessageBox
import {ElMessage} from 'element-plus'
import {remoteLogin} from "@/saga/APi";
import axios from 'axios'

export default {
  name: 'login',
  data() {
    return {
      user: {
        username: 'user1/user2/user3/user4/auditor1/auditor2/auditor3',
        password: '123456'
      }
    }
  },
  methods: {

    loginHandler: function () {
      // axios.get(loginUrl)
      //   .then(( {data} )=>{
      //     console.log("==data==");
      //     console.log(data);

      //     this.$router.push("/main");
      //     alert();
      //   })
      //   .catch((error)=>{
      //     console.log(error);
      //   })

      remoteLogin({username: this.user.username, password: this.user.password}).then((response) => {
        if(response.code==200) {
          var loginUser = response.data;
          sessionStorage.setItem("loginedUser", JSON.stringify(loginUser));
          switch (loginUser.role) {
            case 'customer':
              this.$router.push("/customer");
              break;
            case 'auditor1':
            case 'auditor3':
            case 'auditor2':
              this.$router.push("/auditor");
          }
        } else {
          ElMessage.error('用户名密码错误');
        }

      }).catch((ex) => {
        ElMessage.error('登录服务器connect fail');
      });
    },


    login() {
      //使用axios进行数据提交

      // ElMessageBox.alert('登录成功！', '信息',);
      let loginUrl = "http://127.0.0.1:8010/test";
      console.log("user.username : " + this.user.username);

      console.log("user.password : " + this.user.password);
      this.loginHandler();
    }
  }
}
</script>
<style scoped>
</style>